﻿using System;
using System.Collections.Generic;
using ePlanet.ArchiveBorrow.Models;
using Web.SystemConfig.WorkFlow;
using Web.SystemConfig.SysCache;
using ePlanet.ArchiveBorrow.Enum;
using ePlanet.ArchiveBorrow.Services;
using DreamBuilder.Core.Container;
using System.Data;

namespace Web.Views.ArchiveBorrow.ArchiveBorrowApprove
{
    public partial class ArchiveBorrowBase : System.Web.UI.MasterPage
    {
        #region 私有属性
        private ArchiveBorrowModel.ARCHIVEBORROWRow m_CurrentArchiveBorrow = null;
        /// <summary>
        /// 当前档案借阅对象
        /// </summary>
        public ArchiveBorrowModel.ARCHIVEBORROWRow CurrentArchiveBorrow
        {
            get { return m_CurrentArchiveBorrow; }
            set { m_CurrentArchiveBorrow = value; }
        }

        private ePlanet.ArchiveMange.Enum.ArchiveBaseType m_CurrentArchiveBaseType = ePlanet.ArchiveMange.Enum.ArchiveBaseType.Account;
        /// <summary>
        /// 当前文档类型
        /// </summary>
        public ePlanet.ArchiveMange.Enum.ArchiveBaseType CurrentArchiveBaseType
        {
            get { return m_CurrentArchiveBaseType; }
            set { m_CurrentArchiveBaseType = value; }
        }

        private Web.SystemLogin.Models.User m_CurrentUser
        {
            get
            {
                return (Web.SystemLogin.Models.User)Session["CurrentUser"];
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.m_CurrentArchiveBorrow != null)
                {
                    this.SignData();
                    this.SetOperateButtonVisible((int)this.m_CurrentArchiveBorrow.STATUSID);
                    this.BindData(this.m_CurrentArchiveBorrow);
                }
            }
        }


        #region 按钮事件
        #region 部室领导操作
        /// <summary>
        /// 部门领导不同意
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBBSLDDisAgree_Click(object sender, EventArgs e)
        {
            this.ProcessWorkFlow(ArchiveBorrowEnum.不同意.ToString());
        }

        /// <summary>
        /// 部室领导同意借阅
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBBSLDAgreeBorrow_Click(object sender, EventArgs e)
        {
            this.ProcessWorkFlow(ArchiveBorrowEnum.同意借阅.ToString());
        }

        /// <summary>
        /// 部门领导上报信息中心
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBBSLDToXXZX_Click(object sender, EventArgs e)
        {
            this.ProcessWorkFlow(ArchiveBorrowEnum.送信息中心档案员.ToString());
        }
        #endregion

        #region 档案员操作
        /// <summary>
        /// 信息中心不同意
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnXXZXDAYDisAgree_Click(object sender, EventArgs e)
        {
            this.ProcessWorkFlow(ArchiveBorrowEnum.不同意.ToString());
        }

        /// <summary>
        /// 信息中心上报档案形成部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnXXZXDAYToDAXCBMLD_Click(object sender, EventArgs e)
        {
            if (this.dboToUserList.SelectedIndex != -1)
            {
                string selectUserID = this.dboToUserList.SelectedValue;
                this.ProcessWorkFlow(ArchiveBorrowEnum.送档案形成部门领导.ToString(), selectUserID);
            }
            else
            {
                throw new Exception("请先选择档案形成部门领导！");
            }
        }

        /// <summary>
        /// 借出纸质文档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPaperArchiveLend_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 归还纸质文档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPaperArchiveReturn_Click(object sender, EventArgs e)
        {

        }

        #endregion

        #region 档案形成部门操作
        /// <summary>
        /// 档案形成部门不同意
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDAXCBMLDDisAgree_Click(object sender, EventArgs e)
        {
            this.ProcessWorkFlow(ArchiveBorrowEnum.不同意.ToString());
        }

        /// <summary>
        /// 档案形成部门上报信息中心领导
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDAXCBMToXXZXLD_Click(object sender, EventArgs e)
        {
            this.ProcessWorkFlow(ArchiveBorrowEnum.送信息中心领导.ToString());
        }
        #endregion

        #region 信息中心领导操作
        /// <summary>
        /// 信息中心领导不同意
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnXXZXLDDisAgree_Click(object sender, EventArgs e)
        {
            this.ProcessWorkFlow(ArchiveBorrowEnum.不同意.ToString());
        }

        /// <summary>
        /// 信息中心领导同意办理借阅
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnXXZXLDAgreeBorrow_Click(object sender, EventArgs e)
        {
            this.ProcessWorkFlow(ArchiveBorrowEnum.不同意.ToString());
        }

        #endregion
        #endregion

        #region 私有方法
        #region  数据绑定
        private void SignData()
        {
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            WorkFlow oWorkFlow = new WorkFlow();
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.ArchiveBorrow;
            oWorkFlowModel.RecordID = new Guid(this.m_CurrentArchiveBorrow.DATAID);
            oWorkFlowModel.FromStatusID = (int)this.m_CurrentArchiveBorrow.STATUSID;
            oWorkFlowModel.FromUserID = new Guid(m_CurrentUser.User_ID);
            oWorkFlow.SignData(oWorkFlowModel);
        }

        private void BindData(ArchiveBorrowModel.ARCHIVEBORROWRow row)
        {
            if (!row.IsVALIDPERIODNull())
            {
                this.txtValidPeriod.Text = row.VALIDPERIOD.ToShortDateString();
            }
            if (!row.IsCANDOWNLOADNull())
            {
                this.cboCanDownload.Checked = row.CANDOWNLOAD == 1 ? true : false;
            }
            if (!row.IsCANVIEWNull())
            {
                this.cboCanView.Checked = row.CANVIEW == 1 ? true : false;
            }
            if (!row.IsISPAPERARCHIVENull())
            {
                this.cboIsIsPaperArchive.Checked = row.ISPAPERARCHIVE == 1 ? true : false;
            }
            if (!row.IsISPAPERARCHIVERETURNNull())
            {
                this.cboIsPaperArchiveReturn.Checked = row.ISPAPERARCHIVERETURN == 1 ? true : false;
            }
            if (!row.IsRETURNTIMENull())
            {
                this.txtReturnTime.Text = row.RETURNTIME.ToShortDateString();
            }
            if (!row.IsVALIDPERIODNull())
            {
                this.txtValidPeriod.Text = row.VALIDPERIOD.ToShortDateString();
            }
        }

        private void BindToUserList(int statueID, string operateName)
        {
            WorkFlowMessageStatusRule nextRule = new ArchiveCacheFacade().GetStatusRuleCache().GetRuleByStatusIDAndOperateName(statueID, operateName);
            int toRoleID = nextRule.ToRoleID;
            DataTable toUserList = UContainer.Resolve<IArchiveBorrowService>().GetToUserList(toRoleID);
            this.dboToUserList.DataSource = toUserList;
            this.dboToUserList.DataTextField = "UserName";
            this.dboToUserList.DataValueField = "UserID";
            this.dboToUserList.DataBind();
        }
        #endregion

        #region 操作权限
        /// <summary>
        /// 通过状态，判断文档操作权限
        /// </summary>
        private void SetOperateButtonVisible(int statusID)
        {
            switch (statusID)
            {
                case 501://本部室领导
                    this.SetBBSLDOperate(true);
                    this.SetDAXCBMOperate(false);
                    this.SetXXZXDAYOperate(false);
                    this.SetXXZXLDOperate(false);
                    this.SetPaperArchiveOperate(false);
                    break;
                case 502://信息中心档案员
                    this.SetBBSLDOperate(false);
                    this.SetDAXCBMOperate(false);
                    this.SetXXZXDAYOperate(true);
                    this.SetXXZXLDOperate(false);
                    this.SetPaperArchiveOperate(false);
                    //此状态下，需要选择档案形成部门的领导
                    this.divToUserList.Visible = true;
                    this.BindToUserList(502, ArchiveBorrowEnum.送档案形成部门领导.ToString());
                    this.dboToUserList.Enabled = true;
                    break;
                case 503://档案形成部门审核中
                    this.SetBBSLDOperate(false);
                    this.SetDAXCBMOperate(true);
                    this.SetXXZXDAYOperate(false);
                    this.SetXXZXLDOperate(false);
                    this.SetPaperArchiveOperate(false);
                    break;
                case 504://信息中心领导
                    this.SetBBSLDOperate(false);
                    this.SetDAXCBMOperate(false);
                    this.SetXXZXDAYOperate(false);
                    this.SetXXZXLDOperate(true);
                    this.SetPaperArchiveOperate(false);
                    break;
                case 505://走完流程的数据，拥有纸质文档借阅接触权限
                    this.SetBBSLDOperate(false);
                    this.SetDAXCBMOperate(false);
                    this.SetXXZXDAYOperate(false);
                    this.SetXXZXLDOperate(false);
                    this.SetPaperArchiveOperate(true);
                    break;
                default://异常状态，不拥有操作权限
                    this.SetBBSLDOperate(false);
                    this.SetDAXCBMOperate(false);
                    this.SetXXZXDAYOperate(false);
                    this.SetXXZXLDOperate(false);
                    this.SetPaperArchiveOperate(false);
                    break;
            }
        }

        #region 档案借阅角色
        /// <summary>
        /// 本部室领导操作权限
        /// </summary>
        private void SetBBSLDOperate(bool show)
        {
            ///拥有不同意、上报信息中心档案员、上报档案员办理借阅权限
            //本部室不同意借阅
            this.btnBBSLDDisAgree.Visible = show;
            //送信息中心档案员
            this.btnBBSLDToXXZX.Visible = show;
            //同意办理借阅
            this.btnBBSLDAgreeBorrow.Visible = show;
        }

        /// <summary>
        /// 信息中心档案员操作权限
        /// </summary>
        private void SetXXZXDAYOperate(bool show)
        {
            //拥有上报档案形成单位领导、不同意权限
            this.btnXXZXDAYDisAgree.Visible = show;
            this.btnXXZXDAYToDAXCBMLD.Visible = show;
        }

        /// <summary>
        /// 档案形成部门操作权限
        /// </summary>
        private void SetDAXCBMOperate(bool show)
        {
            //档案形成部门拥有不同意、送信息中心领导操作
            this.btnDAXCBMLDDisAgree.Visible = show;
            this.btnDAXCBMToXXZXLD.Visible = show;
        }

        /// <summary>
        /// 信息中心领导操作权限
        /// </summary>
        private void SetXXZXLDOperate(bool show)
        {
            //信息中心领导拥有不同意、办理借阅操作
            this.btnXXZXLDAgreeBorrow.Visible = show;
            this.btnXXZXLDDisAgree.Visible = show;
        }

        /// <summary>
        /// 纸质文档借阅权限
        /// </summary>
        private void SetPaperArchiveOperate(bool show)
        {
            this.btnPaperArchiveLend.Visible = show;
            this.btnPaperArchiveReturn.Visible = show;
        }
        #endregion
        #endregion

        #region 流程操作
        private void ProcessWorkFlow(string operateName)
        {
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            if (currentUser == null)
            {
                throw new Exception("无法获取该用户的信息！");
            }
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            oWorkFlowModel.OperateLogID = Guid.NewGuid();
            oWorkFlowModel.RecordID = new Guid(this.m_CurrentArchiveBorrow.DATAID);
            oWorkFlowModel.FromStatusID = (int)this.m_CurrentArchiveBorrow.STATUSID;
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.ArchiveBorrow;
            oWorkFlowModel.FromUserID = new Guid(currentUser.User_ID);
            WorkFlowMessageStatusRuleListCache ruleCache = new ArchiveCacheFacade().GetStatusRuleCache();
            WorkFlowMessageStatusRule nextRule = ruleCache.GetRuleByStatusIDAndOperateName((int)this.m_CurrentArchiveBorrow.STATUSID, operateName);
            if (nextRule == null)
            {
                throw new Exception("流程配置错误。");
            }
            oWorkFlowModel.OperateID = nextRule.OperateID;
            WorkFlow oWorkFlow = new WorkFlow();
            oWorkFlow.FlowProcess(oWorkFlowModel);

            if (operateName == ArchiveBorrowEnum.同意借阅.ToString())
            {
                oWorkFlow.FlowFinish(ArchiveTableEnum.ArchiveBorrow, this.m_CurrentArchiveBorrow.DATAID);
            }

            Alert("操作成功！");
        }

        private void ProcessWorkFlow(string operateName, string userID)
        {
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            if (currentUser == null)
            {
                throw new Exception("无法获取该用户的信息！");
            }
            Guid UserID = new Guid(userID);
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            oWorkFlowModel.OperateLogID = Guid.NewGuid();
            oWorkFlowModel.RecordID = new Guid(this.m_CurrentArchiveBorrow.DATAID);
            oWorkFlowModel.FromStatusID = (int)this.m_CurrentArchiveBorrow.STATUSID;
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.ArchiveBorrow;
            oWorkFlowModel.FromUserID = new Guid(currentUser.User_ID);
            WorkFlowMessageStatusRuleListCache ruleCache = new ArchiveCacheFacade().GetStatusRuleCache();
            WorkFlowMessageStatusRule nextRule = ruleCache.GetRuleByStatusIDAndOperateName((int)this.m_CurrentArchiveBorrow.STATUSID, operateName);
            if (nextRule == null)
            {
                throw new Exception("流程配置错误。");
            }
            oWorkFlowModel.OperateID = nextRule.OperateID;
            WorkFlow oWorkFlow = new WorkFlow();
            oWorkFlow.FlowProcess(oWorkFlowModel, UserID);

            if (operateName == ArchiveBorrowEnum.同意借阅.ToString())
            {
                oWorkFlow.FlowFinish(ArchiveTableEnum.ArchiveBorrow, this.m_CurrentArchiveBorrow.DATAID);
            }

            Alert("操作成功！");
        }
        #endregion

        private void Alert(string message)
        {
            Response.Write("<script>alert('" + message + "');</script>");
        }
        #endregion
    }
}